package ruleengine

// Context 上下文
type Context struct {
	Variables map[string]interface{} // 变量存储
	Functions map[string]func(args ...interface{}) interface{} // 函数存储
}

// NewContext 创建上下文
func NewContext() *Context {
	return &Context{
		Variables: make(map[string]interface{}),
		Functions: make(map[string]func(args ...interface{}) interface{}),
	}
}

// SetVariable 设置变量
func (c *Context) SetVariable(name string, value interface{}) {
	c.Variables[name] = value
}

// GetVariable 获取变量
func (c *Context) GetVariable(name string) interface{} {
	if v, ok := c.Variables[name]; ok {
		return v
	}
	return nil
}

func (c  *Context) RegisterFunction(name string, fn func(args ...interface{}) interface{})  {
	c.Functions[name] = fn
}

func (c  *Context) CallFunction(name string, args ...interface{}) interface{} {
	if fn, ok := c.Functions[name]; ok {
		return fn(args...)
	}
	return nil
}
